package com.szwistar.common.os;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTime extends Date {
	private static final long serialVersionUID = -775965195654579679L;

	public DateTime() {
		super();
	}

	public DateTime(Date time) {
		super(time.getTime());
	}

	public DateTime(long ms) {
		super(ms);
	}

	/**
	 * 取得当前日期时间(ms数)
	 */
	public static long nowTime() {
		return now().getTime();
	}

	/**
	 * 取得当前日期时间
	 */
	public static DateTime now() {
		return new DateTime();
	}

	/**
	 * 取得当前日期时间格式化字符串(默认格式)
	 */
	public static String formatNow() {
		return new DateTime().format();
	}

	/**
	 * 取得当前日期时间ms 毫秒之后的时间
	 * @param ms 时间 ms
	 */
	public static DateTime afterNow(long ms) {
		return new DateTime(System.currentTimeMillis() + ms);
	}

	/**
	 * 取得今天凌晨时间
	 */
	public static DateTime today() {
		Calendar date = Calendar.getInstance();
		date.set(Calendar.AM_PM, Calendar.AM);
		date.set(Calendar.HOUR, 0);
		date.set(Calendar.MINUTE, 0);
		date.set(Calendar.SECOND, 0);
		date.set(Calendar.MILLISECOND, 0);
		return new DateTime(date.getTimeInMillis());
	}

	/**
	 * 今天的某个时间
	 * @param ms 时间 ms
	 */
	public static DateTime today(long ms) {
		return new DateTime(today().getTime() + ms);
	}

	/**
	 * 今天的某个时间
	 * @param time 格式为"HH:mm:ss"，如"13:30:25"
	 */
	public static DateTime atToday(String time) throws ParseException {
	    SimpleDateFormat simpleDate = new SimpleDateFormat("HH:mm:ss");
	    simpleDate.parse(time);
	    Calendar delta = simpleDate.getCalendar();

		Calendar date = Calendar.getInstance();
		date.set(Calendar.AM_PM, delta.get(Calendar.AM_PM));
		date.set(Calendar.HOUR, delta.get(Calendar.HOUR));
		date.set(Calendar.MINUTE, delta.get(Calendar.MINUTE));
		date.set(Calendar.SECOND, delta.get(Calendar.SECOND));
		date.set(Calendar.MILLISECOND, 0);
		return new DateTime(date.getTimeInMillis());
	}

	/**
	 * 今天到现在过去了多少毫秒
	 * @return 毫秒
	 */
	public static long todayElapsedMs() {
		return now().getTime() % (24*60*60*1000);
	}

	/**
	 * 取得指定的日期时间
	 * @param time 指定的日期时间字符串，格式为："yyyy-MM-dd HH:mm:ss"，如"2018-12-03 13:30:25"
	 * @throws ParseException
	 */
	public static DateTime at(String time) throws ParseException {
	    SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return new DateTime(simpleDate.parse(time).getTime());
	}

	/**
	 * 格式化
	 * @param pattern 如果为 null，则使用标准的格式字符串："yyyy-MM-dd HH:mm:ss"
	 * @return 日期字符串
	 */
    public String format(String pattern) {
    	if(pattern == null) { pattern = "yyyy-MM-dd HH:mm:ss"; }
    	SimpleDateFormat dateFormatter = new SimpleDateFormat(pattern);
		return dateFormatter.format(this);
    }

    public String format() {
    	return format((String)null);
    }

    public String formatDate() {
		return format("yyyy-MM-dd");
    }

    public String formatTime() {
		return format("HH:mm:ss");
    }

    public static String format(Date date, String pattern) {
		return new DateTime(date).format(pattern);
    }

    public static String format(Date date) {
    	return format(date, null);
    }

	public static String format(long time, String pattern) {
		return new DateTime(time).format(pattern);
	}

    public static String format(long time) {
    	return format(time, null);
    }

    public static long elapsed(long time) {
    	return System.currentTimeMillis() - time;
    }

    public static boolean timeout(long time, long timeout) {
    	return (System.currentTimeMillis() - time) > timeout;
    }

	public static void main(String[] args) throws Exception {
		System.out.println(now());
		System.out.println(today());
		System.out.println(at("2018-12-03 13:30:25"));
		System.out.println(atToday("13:30:25"));
		System.out.println(todayElapsedMs());
		System.out.println(DateTime.at("2999-12-31 23:59:59").getTime());
	}
}
